#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct st_node {
    char data[128];
    struct st_node *prev;
    struct st_node *next;
} *Node;

/** Biref Description
 * Description
 * @param 
 * @see 
 * @return 
 */

Node node_new (const char *data)
{

    Node node = (Node)malloc(sizeof(struct st_node));

    node->prev = NULL;
    node->next = NULL;
    strcpy(node->data, data);

    return node;
}

/** Biref Description
 * Description
 * @param 
 * @see 
 * @return 
 */

int node_free (Node node)
{

    free(node);

    return 0;
}

/** Biref Description
 * Description
 * @param 
 * @see 
 * @return 
 */

int node_print (Node node)
{

    printf("%s\n", node->data);
    //printf("PrevPtr: %p\n", node->prev);
    //printf("NextPrt: %p\n", node->next);

    return 0;
}

/** Biref Description
 * Description
 * @param 
 * @see 
 * @return 
 */

Node list_new (void)
{

    Node nil = node_new("nil");
    nil->prev = nil;
    nil->next = nil;

    return nil;
}

/** Biref Description
 * Description
 * @param 
 * @see 
 * @return 
 */

int list_insert(Node head, Node n)
{

    n->next = head->next;
    n->prev = head;
    head->next = n;
    n->next->prev = n;

    return 0;
}

/** Biref Description
 * Description
 * @param 
 * @see 
 * @return 
 */

Node list_remove (Node list, Node n)
{

    n->prev->next = n->next;
    n->next->prev = n->prev;

    return n;
}

/** Biref Description
 * Description
 * @param 
 * @see 
 * @return 
 */

int list_free (Node head)
{

    while (head->next != head) {
        Node t = head->next;
        head->next = t->next;
        node_free(t);
    }
    node_free(head);

    return 0;
}

int main (int argc, char const* argv[])
{
    char buf[128];

    Node list = list_new();
    Node default_url = node_new("http://www.acm.org/");
    Node curptr;

    list_insert(list, default_url);
    curptr = default_url;

    while (EOF != scanf("%s", buf)) {
        if (0 == strcmp(buf, "VISIT")) {
            scanf("%s", buf);
            Node t = node_new(buf);
            Node tmp = curptr->prev;
            while (tmp != list) {
                list_remove(list, tmp);
                node_free(tmp);
                tmp = curptr->prev;
            }
            list_insert(list, t);
            curptr = t;
            node_print(curptr);
        } else if (0 == strcmp(buf, "BACK")) {
            if (curptr->next!= list) {
                curptr = curptr->next;
                node_print(curptr);
            } else {
                printf("Ignored\n");
            }
        } else if (0 == strcmp(buf, "FORWARD")) {
            if (curptr->prev != list) {
                curptr = curptr->prev;
                node_print(curptr);
            } else {
                printf("Ignored\n");
            }
        } else if (0 == strcmp(buf, "QUIT")) {
            break;
        }
    }

    list_free(list);

    return 0;
}
